﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using com.pro2e.console.service.auth.vo;
using com.pro2e.console.service.auth;
using System.Collections.Generic;
using com.pro2e.console.service.msg.vo;

public partial class adm_UC14_UC14_3_UserRoleSet : System.Web.UI.Page
{
    AuthService authService = new AuthService();
    log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    Hashtable toBeApplyRole = new Hashtable();

    protected void Page_Load(object sender, EventArgs e)
    {
        string userId = Request.QueryString["UserId"];
        if (!Page.IsPostBack) 
        {
            initData(userId);
        }
    }

    
    protected void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
    {
        string userId = ddlUser.SelectedValue;
        initData(userId);
    }
   
    protected void btnRemove_Click(object sender, EventArgs e)
    {
        lblMsg.Text = "";
        int selectedIndex = lbxHadRole.SelectedIndex;
        log.Debug("selected user = " + selectedIndex.ToString());

        if (selectedIndex != -1)
        {

            ListItem selectedItem = lbxHadRole.SelectedItem;
            lbxHadRole.Items.Remove(selectedItem);
            lblxToBeRole.Items.Add(selectedItem);
        }
        lbxHadRole.DataBind();
        lblxToBeRole.DataBind();

    }

    private void initData(string userId)
    {
        lblMsg.Text = "";
        ddlUser.Items.Clear();
        lblxToBeRole.Items.Clear();
        lbxHadRole.Items.Clear();

        initDDlUser(userId);
        log.Debug("original selected user = " + ddlUser.SelectedValue);
        string selectedUserId = ddlUser.SelectedValue;
        initRight(selectedUserId);
        initLeft(selectedUserId);

   }

    /// <summary>
    /// 初始化右邊視窗
    /// </summary>
    /// <param name="selectedUserId"></param>
    private void initLeft(string selectedUserId)
    {
        IList<LoginRole> allRoleList = authService.GetAllRoles();

        LoginUser loginUser = authService.LoadLoginUser(selectedUserId);

        foreach (LoginRole role in allRoleList)
        {          
            if (loginUser.BelongRoles == null || !loginUser.BelongRoles.Contains(role))
            {
                ListItem item = new ListItem(role.RoleName, role.RoleId.ToString());
                lblxToBeRole.Items.Add(item);
            }
        }

        lblxToBeRole.DataBind();
    }


    /// <summary>
    /// 初始化左邊視窗
    /// </summary>
    /// <param name="selectedUserId"></param>
    private void initRight(string selectedUserId)
    {
        LoginUser loginUser = authService.LoadLoginUser(selectedUserId);
        IList<LoginRole> roleList = loginUser.BelongRoles;
        if (roleList != null)
        {
            foreach (LoginRole role in roleList)
            {
                ListItem item = new ListItem(role.RoleName, role.RoleId.ToString());
                lbxHadRole.Items.Add(item);
            }
        }

        lbxHadRole.DataBind();

    }

    /// <summary>
    /// 初始化User下拉
    /// </summary>
    /// <param name="userId"></param>
    private void initDDlUser(string userId)
    {
        IList<LoginUser> users = authService.GetAllUsers();
        foreach (LoginUser user in users) 
        {
            ListItem item = new ListItem(user.FullNameInChinese, user.UserId);
            ddlUser.Items.Add(item);
          
        }

        if (string.IsNullOrEmpty(userId))
        {
            ddlUser.SelectedIndex = 0;
        }
        else
        {
            ddlUser.SelectedValue = userId;
        }
        
        ddlUser.DataBind();
    }

    protected void lblxToBeRole_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void lbxHadRole_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        lblMsg.Text = "";
        int selectedIndex = lblxToBeRole.SelectedIndex;
        log.Debug("selected user = " + selectedIndex.ToString());

        if (selectedIndex != -1) 
        {

            ListItem selectedItem = lblxToBeRole.SelectedItem;
            lblxToBeRole.Items.Remove(selectedItem);
            lbxHadRole.Items.Add(selectedItem);
        }  
        lbxHadRole.DataBind();
        lblxToBeRole.DataBind();
    }
    protected void Button3_Click(object sender, ImageClickEventArgs e)
    {
        string userId = ddlUser.SelectedValue;
        LoginUser user = authService.LoadLoginUser(userId);
        List<LoginRole> loginRoleList = new List<LoginRole>();

        foreach (ListItem item in lbxHadRole.Items)
        {
            loginRoleList.Add(authService.GetLoginRole(int.Parse(item.Value)));
        }

        user.BelongRoles = loginRoleList;

        authService.UpdateUser(user);

        //更新快取
        UserMenuFuncContainer.GetInstance().ResetAll();

        lblMsg.Text = MsgVO.UPDATE_OK;
    }
}
